package easy;

public class Question7 {

    public static void main(String[] args) {
        Question7 question7 = new Question7();
        System.out.println(question7.reverse(-10));
//        System.out.println(question7.reverse(123));
//        System.out.println(question7.reverse(-123));
//        System.out.println(question7.reverse(120));
//        System.out.println(question7.reverse(1534236469));
    }

    public int reverse(int x) {
        int result = 0;
        if (x == 0) {
            return result;
        }
        try {
            if (x > 0) {
                while (x != 0) {
                    if (result > (Integer.MAX_VALUE - x % 10) / 10) {
                        throw new RuntimeException();
                    }
                    result = result * 10 + x % 10;
                    x /= 10;
                }
            } else {
                while (x != 0) {
                    if (result < (Integer.MIN_VALUE - x % 10) / 10) {
                        throw new RuntimeException();
                    }
                    result = result * 10 + x % 10;
                    x /= 10;
                }
            }
        } catch (Exception ignored) {
            return 0;
        }
        return result;
    }
}
